/*
 * @User 		@Company 	@Date
 * marcusbui	VNNET24  	Jun 19, 2011
 */
package tsb.com.html5.controller.common;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import tsb.com.html5.comment.item.CommentItem;
import tsb.com.html5.comment.param.GetCommentParam;
import tsb.com.html5.comment.service.ICommentService;
import tsb.com.html5.controller.base.BaseController;
import tsb.com.html5.mail.IMailManager;

// TODO: Auto-generated Javadoc
/**
 * The Class CommentController.
 */
@Controller
@RequestMapping("/comment")
public class CommentController extends BaseController{

	/** The comment service. */
	@Resource(name="CommentService")
	private ICommentService commentService;
	
	/**
	 * Search comment.
	 *
	 * @param id the id
	 * @return the map< string,? extends object>
	 * @throws Exception the exception
	 */
	@RequestMapping(value="/search", method = RequestMethod.GET)
	protected @ResponseBody Map<String, ? extends Object> searchComment(@RequestParam String id) throws Exception {
		HashMap<String, CommentItem> returnMap = new HashMap<String, CommentItem>(); 
		try {
			if(id!=null){
				GetCommentParam commentParam = new GetCommentParam();
				commentParam.setId(id);
				
				CommentItem updatedCommentItem = commentService.getCommentById(commentParam);
				if(updatedCommentItem!=null){
					returnMap.put("comment", updatedCommentItem);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return returnMap;
	}
	
	//BindingResult result, Model model
	/**
	 * Creates the comment.
	 *
	 * @param commendItem the commend item
	 * @param request the request
	 * @param response the response
	 * @return the map< string,? extends object>
	 * @throws Exception the exception
	 */
	@RequestMapping(value="/create", method = RequestMethod.POST)
	protected @ResponseBody Map<String, ? extends Object> createComment(@RequestBody CommentItem commendItem, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap<String, CommentItem> returnMap = new HashMap<String, CommentItem>(); 
		try {
			
			String mailContent = ""
				+"<br/>From: " + commendItem.getUserId()
				+"<br/>Email: " + commendItem.getEmail()
				+"<br/>Company: " + commendItem.getCompany()
				+"<br/>Content: " + commendItem.getContent()
				+"<br/>"
				+""
				+""
				+"";
			
			IMailManager mailManager = (IMailManager)this.getBean("mailManager");
			mailManager.SendMail(mailContent);
			mailManager.SmsAlert();
			System.out.print("");
			
			CommentItem updatedItem = commentService.createComment(commendItem);
			if(updatedItem!=null){
				returnMap.put("comment", updatedItem);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return returnMap;
	}
	
	/**
	 * Delete comment.
	 *
	 * @param id the id
	 * @return true, if successful
	 * @throws Exception the exception
	 */
	@RequestMapping(value="/delete", method = RequestMethod.GET)
	protected @ResponseBody boolean deleteComment(@RequestParam String id) throws Exception {
		boolean success = false;
		try {
			if(id!=null){
				CommentItem commentItem = new CommentItem();
				commentItem.setSeq(id);
				
				success = commentService.deleteComment(commentItem);
			}
		} catch (Exception e) {
			success = false;
			e.printStackTrace();
			throw e;
		}
		return success;
	}
	
	/**
	 * Update comment.
	 *
	 * @param commendItem the commend item
	 * @param request the request
	 * @param response the response
	 * @return the map< string,? extends object>
	 * @throws Exception the exception
	 */
	@RequestMapping(value="/update", method = RequestMethod.POST)
	protected @ResponseBody Map<String, ? extends Object> updateComment(@RequestBody CommentItem commendItem, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap<String, CommentItem> returnMap = new HashMap<String, CommentItem>(); 
		try {
			
			CommentItem updatedItem = commentService.updateComment(commendItem);
			if(updatedItem!=null){
				returnMap.put("comment", updatedItem);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return returnMap;
	}
	
	
}
